package com.orangelife.service.propFeePreBill.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.cerHouseholder.ICerHouseholderService;
import com.orangelife.service.cerHouseholder.model.CerHouseholderDto;
import com.orangelife.service.offset.IOffsetService;
import com.orangelife.service.propFeeAccount.IPropFeeAccountService;
import com.orangelife.service.propFeeAccount.model.AmountHandleDto;
import com.orangelife.service.propFeeAccount.model.PropFeeAccountDto;
import com.orangelife.service.propFeeAmountLog.model.PropFeeAmountLogDto;
import com.orangelife.service.propFeeComm.IPropFeeCommService;
import com.orangelife.service.propFeeComm.model.PayBillInfo;
import com.orangelife.service.propFeeOffsetInfo.IPropFeeOffsetInfoService;
import com.orangelife.service.propFeePreBill.IPropFeePreBillService;
import com.orangelife.service.propFeePreBill.model.PropFeePreBillDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.HttpSvcUtils;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.PropertyUtils;
import com.orangelife.utils.StringUtils;
import com.orangelife.utils.UUIDGenerator;
import com.orangelife.utils.UrlPath;
import com.orangelife.webservice.service.propFeeOffsetInfo.entity.OffSetDto;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeePreBill类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-11-24 14:03:57
 * ==============================================================
 */
@Service("propFeePreBillService")
public class PropFeePreBillServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeePreBillService {
	private static Logger logger = Logger
			.getLogger(PropFeePreBillServiceImpl.class);
	@Resource(name = "cerHouseholderService")
	private ICerHouseholderService cerHouseholderService;
	@Resource(name = "propFeeAccountService")
	private IPropFeeAccountService propFeeAccountService;

	@Resource(name = "propFeeCommService")
	private IPropFeeCommService propFeeCommService;

	@Resource(name = "offsetService")
	private IOffsetService offsetService;
	@Resource(name = "propFeeOffsetInfoService")
	private IPropFeeOffsetInfoService propFeeOffsetInfoService;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeePreBillServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeePreBill(PropFeePreBillDto propFeePreBill)
			throws AppBizException {

		Assert.notNull(propFeePreBill);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeePreBill.getReceiptNum())) {
			if (propFeePreBill.getReceiptNum().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "收据编码长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePreBill.getPayType())) {
			if (propFeePreBill.getPayType().length() > 8) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "支付方式长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePreBill.getStatus())) {
			if (propFeePreBill.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "状态长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePreBill.getPayer())) {
			if (propFeePreBill.getPayer().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "付款人长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePreBill.getReceiver())) {
			if (propFeePreBill.getReceiver().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "收款人长度超出");
			}
		}

		if (StringUtils.isBlank(propFeePreBill.getPreBillID())) {
			propFeePreBill.setPreBillID(UUIDGenerator.nextCode());
		}
		if (StringUtils.isBlank(propFeePreBill.getStatus())) {
			propFeePreBill.setStatus("1");
		}
		propFeePreBill.setItemStat(TypeConst.FLAG_ON);
		propFeePreBill.setTscreate(new Date());
		propFeePreBill.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeePreBillMapper.insertSelective", propFeePreBill);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeePreBill(PropFeePreBillDto propFeePreBill)
			throws AppBizException {

		Assert.notNull(propFeePreBill);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeePreBill.getPreBillID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeePreBillDto oldPropFeePreBill = this
				.findPropFeePreBillByID(propFeePreBill.getPreBillID());

		if (!StringUtils.isBlank(propFeePreBill.getReceiptNum())) {
			if (propFeePreBill.getReceiptNum().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "收据编码长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePreBill.getPayType())) {
			if (propFeePreBill.getPayType().length() > 8) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "支付方式长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePreBill.getStatus())) {
			if (propFeePreBill.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "状态长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePreBill.getPayer())) {
			if (propFeePreBill.getPayer().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "付款人长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePreBill.getReceiver())) {
			if (propFeePreBill.getReceiver().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "收款人长度超出");
			}
		}

		if (oldPropFeePreBill == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeePreBill.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeePreBillMapper.updateByPrimaryKeySelective",
				propFeePreBill);
	}

	@Override
	public PropFeePreBillDto findPropFeePreBillByID(String propFeePreBillID)
			throws AppBizException {
		if (StringUtils.isBlank(propFeePreBillID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeePreBillDto oldPropFeePreBill = this.getSqlSession().selectOne(
				"PropFeePreBillSearchMapper.selectByPrimaryKey",
				propFeePreBillID);
		if (oldPropFeePreBill == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeePreBill.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeePreBill;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeePreBillByLogic(String propFeePreBillID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeePreBillID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeePreBillDto oldPropFeePreBill = this
				.findPropFeePreBillByID(propFeePreBillID);

		oldPropFeePreBill.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeePreBill.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeePreBillMapper.updateByPrimaryKeySelective",
				oldPropFeePreBill);
	}

	@Override
	public List<Map<String, Object>> findPropFeePreBillByPage(String type,
			String commID, String searchType, String startTime, String endTime,
			String payTypes, String tsCreate, String search, String status,
			int pageNo, int pageSize) throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/

		Map<String, Object> map = new HashMap<String, Object>();
		if (!StringUtils.isBlank(startTime)) {
			map.put("startTime", Long.parseLong(startTime) / 1000);
		}
		if (!StringUtils.isBlank(endTime)) {
			map.put("endTime", Long.parseLong(endTime) / 1000);
		}
		if (!StringUtils.isBlank(payTypes)) {
			map.put("payTypes", payTypes.replaceAll("#", ","));
		}
		map.put("commID", commID);
		if (!StringUtils.isBlank(searchType)) {
			map.put("searchType", String.valueOf(searchType));
		}
		map.put("tsCreate", tsCreate);
		map.put("type", type);
		map.put("search", search);
		map.put("status", status);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeePreBillSearchMapper.findAllPropFeePreBill", map);
		return list;
	}

	@Override
	public List<PropFeePreBillDto> getPropFeePreBillByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeePreBillDto> list = this.getSqlSession().selectList(
				"PropFeePreBillSearchMapper.getAllPropFeePreBillDto", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeePreBillByRelevantID(
			String preBillID, String commID, String apartmentID,
			String payType, String status, String payerID, String receiverID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("preBillID", preBillID);
		map.put("commID", commID);
		map.put("apartmentID", apartmentID);
		map.put("payType", payType);
		map.put("status", status);
		map.put("payerID", payerID);
		map.put("receiverID", receiverID);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeePreBillSearchMapper.selectPropFeePreBillDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeePreBillDto> findPropFeePreBillByRelevantID(
			String preBillID, String commID, String apartmentID,
			String payType, String status, String payerID, String receiverID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("preBillID", preBillID);
		map.put("commID", commID);
		map.put("apartmentID", apartmentID);
		map.put("payType", payType);
		map.put("status", status);
		map.put("payerID", payerID);
		map.put("receiverID", receiverID);

		List<PropFeePreBillDto> list = this.getSqlSession().selectList(
				"PropFeePreBillSearchMapper.findPropFeePreBillDtoByRelevantID",
				map);
		return list;
	}

	@Override
	public List<PropFeePreBillDto> bauthfindPropFeePreBill(
			List<String> propFeePreBillIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeePreBillDto> list = this.getSqlSession().selectList(
				"PropFeePreBillSearchMapper.batchFindPropFeePreBill",
				propFeePreBillIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeePreBillByMap(
			Map<String, Object> propFeePreBill) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeePreBillSearchMapper.batchFindPropFeePreBillByMap",
				propFeePreBill);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeePreBillInfo(PropFeePreBillDto propFeePreBill)
			throws AppBizException {

		if (StringUtils.isBlank(propFeePreBill.getApartmentID())) {
			throw new AppBizException(2, "亲，房屋信息");
		}

		if (StringUtils.isBlank(propFeePreBill.getCommID())) {
			throw new AppBizException(2, "亲，小区信息");
		}

		if (propFeePreBill.getMoney() == null) {
			throw new AppBizException(2, "亲，充值金额");
		}

		/*
		 * if (StringUtils.isBlank(propFeePreBill.getPayerID())) { throw new
		 * AppBizException(2, "亲，付款人信息"); }
		 */
		if (StringUtils.isBlank(propFeePreBill.getPayType())) {
			throw new AppBizException(2, "亲，付款类型信息");
		}
		/*
		 * if (propFeePreBill.getPayType().equals("0") &&
		 * propFeePreBill.getPayType().equals("1") &&
		 * propFeePreBill.getPayType().equals("2") &&
		 * propFeePreBill.getPayType().equals("3")) { throw new
		 * AppBizException(2, "亲，付款类型信息"); }
		 */
		CerHouseholderDto cerHouseholderDto = new CerHouseholderDto();
		if (!StringUtils.isBlank(propFeePreBill.getPayerID())) {
			cerHouseholderDto = cerHouseholderService
					.findCerHouseholderByID(propFeePreBill.getPayerID());
		}

		List<PropFeeAccountDto> propFeeAccountDtos = propFeeAccountService
				.findPropFeeAccountByRelevantID(null,
						propFeePreBill.getApartmentID(), "1",propFeePreBill.getAccountTypeCode());
		PropFeeAccountDto propFeeAccountDto = new PropFeeAccountDto();
		if (propFeeAccountDtos == null || propFeeAccountDtos.size() <= 0) {

			propFeeAccountDto.setApartmentID(propFeePreBill.getApartmentID());
			propFeeAccountDto.setMoney(new BigDecimal("0"));
			propFeeAccountDto.setStatus("1");
			propFeeAccountDto.setCreateuserUser(propFeePreBill
					.getCreateuserID());
			propFeeAccountDto.setAccountID(IdUtil.getuuId());
			propFeeAccountService.addPropFeeAccount(propFeeAccountDto);
		} else {
			propFeeAccountDto = propFeeAccountDtos.get(0);
		}
		propFeePreBill.setAccountID(propFeeAccountDto.getAccountID());
		propFeePreBill.setPreBillID(UUIDGenerator.nextCode());
		propFeePreBill.setPayer(cerHouseholderDto.getName());
		propFeeAccountDto.setOperation("1");
		propFeeAccountDto.setMoney(propFeePreBill.getMoney());
		propFeeAccountDto.setRemark("用户 "
				+ (StringUtils.isBlank(propFeePreBill.getPayer()) ? ""
						: propFeePreBill.getPayer()) + "往房屋余额账户添加物业费预存款  "
				+ propFeePreBill.getMoney() + " 操作人"
				+ propFeePreBill.getReceiver());
		propFeeAccountDto.setCreateuserUser(cerHouseholderDto.getName());

		propFeeAccountDto.setItem("预存物业费");
		propFeeAccountDto.setStatementType(propFeePreBill.getPayType());
		propFeeAccountDto.setLinkOrderID(propFeePreBill.getPreBillID());
		propFeeAccountService.editPropFeeAccount(propFeeAccountDto);

		propFeePreBill.setBillTime(new Date());
		int n = this.addPropFeePreBill(propFeePreBill);

		if (n > 0) {
			// sendInfoToFinal(propFeePreBill, propFeeAccountDto,
			// cerHouseholderDto);
		}

		return n;

	}

	/***
	 * 发送消息去财务中心
	 * 
	 * @param propFeePreBill
	 * @param propFeeAccountDto
	 * @param cerHouseholderDto
	 * @throws AppBizException
	 */
	public void sendInfoToFinal(PropFeePreBillDto propFeePreBill,
			PropFeeAccountDto propFeeAccountDto,
			CerHouseholderDto cerHouseholderDto) throws AppBizException {

		try {

			PayBillInfo payBillInfo = new PayBillInfo();
			payBillInfo.setTsBill(String.valueOf(propFeePreBill.getBillTime()
					.getTime()));
			payBillInfo.setApartmentID(propFeePreBill.getApartmentID());
			payBillInfo.setMoney(propFeePreBill.getMoney().toString());
			payBillInfo.setCommID(propFeePreBill.getCommID());
			payBillInfo.setTsCharge(String.valueOf(propFeePreBill.getBillTime()
					.getTime()));
			payBillInfo.setBillID(propFeePreBill.getPreBillID());
			payBillInfo.setChargeType(propFeePreBill.getPayType());
			payBillInfo.setChargeID(propFeePreBill.getPreBillID());
			payBillInfo.setChargeUserID(propFeePreBill.getCreateuserID());
			payBillInfo.setChargeUserName(propFeePreBill.getReceiver());
			payBillInfo.setRuleType("1");
			payBillInfo.setPayUserID(propFeePreBill.getPayerID());
			payBillInfo.setPayUserName(cerHouseholderDto.getName());
			payBillInfo.setPayPhone(cerHouseholderDto.getPhone());
			payBillInfo.setRemark(propFeeAccountDto.getRemark());
			/***
			 * "opypte":""//操作类型 add：新增 update：修改 delete：删除
			 */
			payBillInfo.setOpypte("add");

			final String accessToken = propFeePreBill.getAccessToken();
			PayBillInfo payBill = propFeeCommService
					.getPayBillInfo(payBillInfo);
			final Map<String, Object> par = new HashMap<String, Object>();
			List<PayBillInfo> payBillInfos = new ArrayList<PayBillInfo>();
			payBillInfos.add(payBill);
			par.put("sourceList", payBillInfos);
			try {
				Thread thread = new Thread(new Runnable() {

					@Override
					public void run() {
						HttpSvcUtils.doPostInfoSvc(UrlPath.sendInfoToFinance,
								par, accessToken);
					}
				});
				thread.start();

			} catch (Exception e) {
				logger.error(e.getMessage());
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		}

	}

	@Override
	public void sendInfoTozEditFinal(PropFeePreBillDto propFeePreBill)
			throws AppBizException {

		try {

			PayBillInfo payBillInfo = new PayBillInfo();
			payBillInfo.setTsBill(String.valueOf(propFeePreBill.getBillTime()
					.getTime()));
			payBillInfo.setApartmentID(propFeePreBill.getApartmentID());
			payBillInfo.setMoney(propFeePreBill.getMoney().toString());
			payBillInfo.setCommID(propFeePreBill.getCommID());
			payBillInfo.setTsCharge(String.valueOf(propFeePreBill.getBillTime()
					.getTime()));
			payBillInfo.setBillID(propFeePreBill.getPreBillID());
			payBillInfo.setChargeType(propFeePreBill.getPayType());
			payBillInfo.setChargeID(propFeePreBill.getPreBillID());
			payBillInfo.setChargeUserID(propFeePreBill.getCreateuserID());
			payBillInfo.setChargeUserName(propFeePreBill.getReceiver());
			payBillInfo.setRuleType("1");
			payBillInfo.setPayUserID(propFeePreBill.getPayerID());
			payBillInfo.setPayUserName(propFeePreBill.getPayer());
			CerHouseholderDto cerHouseholderDto = cerHouseholderService
					.findCerHouseholderByID(propFeePreBill.getPayerID());
			payBillInfo.setPayPhone(cerHouseholderDto.getPhone());
			payBillInfo.setRemark(propFeePreBill.getRemark());
			/***
			 * "opypte":""//操作类型 add：新增 update：修改 delete：删除
			 */
			payBillInfo.setOpypte("update");

			final String accessToken = propFeePreBill.getAccessToken();
			PayBillInfo payBill = propFeeCommService
					.getPayBillInfo(payBillInfo);
			final Map<String, Object> par = new HashMap<String, Object>();
			List<PayBillInfo> payBillInfos = new ArrayList<PayBillInfo>();
			payBillInfos.add(payBill);
			par.put("sourceList", payBillInfos);
			try {
				Thread thread = new Thread(new Runnable() {

					@Override
					public void run() {
						HttpSvcUtils.doPostInfoSvc(UrlPath.sendInfoToFinance,
								par, accessToken);
					}
				});
				thread.start();

			} catch (Exception e) {
				logger.error(e.getMessage());
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		}

	}

	@Override
	public Map<String, Object> getTotalAmount(String type, String commID,
			String searchType, String startTime, String endTime,
			String payTypes, String tsCreate, String search)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/

		Map<String, Object> map = new HashMap<String, Object>();
		if (!StringUtils.isBlank(startTime)) {
			map.put("startTime", Long.parseLong(startTime) / 1000);
		}
		if (!StringUtils.isBlank(endTime)) {
			map.put("endTime", Long.parseLong(endTime) / 1000);
		}
		if (!StringUtils.isBlank(payTypes)) {
			map.put("payTypes", payTypes.replaceAll("#", ","));
		}
		map.put("commID", commID);
		if (!StringUtils.isBlank(searchType)) {
			map.put("searchType", String.valueOf(searchType));
		}
		map.put("tsCreate", tsCreate);
		map.put("type", type);

		map.put("search", search);

		Map<String, Object> list = this.getSqlSession().selectOne(
				"PropFeePreBillSearchMapper.getTotalAmount", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> synchPreBillData(Map<String, Object> map)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		if (map.containsKey("tsedit")) {
			if (map.get("tsedit") != null) {
				if (!StringUtils.isInt(map.get("tsedit").toString())) {
					throw new AppBizException(2, "参数类型错误");
				}
				map.put("tsedit",
						Long.parseLong(map.get("tsedit").toString()) / 1000);

			}
		}
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeePreBillSearchMapper.synchPreBillData", map);
		return list;
	}

	@Override
	public PropFeePreBillDto findPropFeePreBillByOrderID(String orderID)
			throws AppBizException {
		if (StringUtils.isBlank(orderID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeePreBillDto oldPropFeePreBill = this.getSqlSession().selectOne(
				"PropFeePreBillSearchMapper.selectByOrderID", orderID);

		return oldPropFeePreBill;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public synchronized int cancelPropFeePreBill(
			PropFeePreBillDto propFeePreBill) throws AppBizException {
		PropFeePreBillDto oldPropFeePreBill = this
				.findPropFeePreBillByID(propFeePreBill.getPreBillID());
		if ("2".equals(oldPropFeePreBill.getStatus())) {
			throw new AppBizException(98, "已撤销");

		}
		oldPropFeePreBill.setCancelMan(propFeePreBill.getCancelMan());
		oldPropFeePreBill.setCancelTime(propFeePreBill.getCancelTime());
		oldPropFeePreBill.setCancelManID(propFeePreBill.getCancelManID());
		oldPropFeePreBill.setStatus("2");
		PropFeeAccountDto p = propFeeAccountService
				.findPropFeeAccountByID(oldPropFeePreBill.getAccountID());
		if (p == null) {

			throw new AppBizException(98, "账户不存在");
		}
		/***
		 * 指定费用撤销
		 */
		if (oldPropFeePreBill.getType().equals("2")) {
			List<Map<String, Object>> offSetDtos = PropertyUtils
					.turnToEntity(propFeeOffsetInfoService.getOffsetInfo(
							oldPropFeePreBill.getPreBillID(), "1"));

			List<String> offsetIDs = new ArrayList<String>();
			for (Map<String, Object> map : offSetDtos) {
				offsetIDs.add(map.get("offsetID").toString());

			}

			if (offsetIDs.size() > 0) {
				OffSetDto offSetDto = new OffSetDto();
				offSetDto.setCancelMan(propFeePreBill.getCancelMan());
				offSetDto.setCancelManID(propFeePreBill.getCancelManID());
				String[] ids = new String[offsetIDs.size()];

				offSetDto.setOffsetIDs(offsetIDs.toArray(ids));
				offsetService.cancelOffSet(offSetDto);
			}
		}
		p = propFeeAccountService.findPropFeeAccountByID(oldPropFeePreBill
				.getAccountID());

		if (p.getMoney().compareTo(oldPropFeePreBill.getMoney()) < 0) {

			throw new AppBizException(2, "账户余额不足");

		}

		AmountHandleDto a = new AmountHandleDto();
		a.setAccountID(p.getAccountID());
		a.setMoney(oldPropFeePreBill.getMoney());
		a.setRemark("预存款账单撤销");
		a.setItem("预存款");
		a.setApartmentID(p.getApartmentID());
		a.setLinkID(oldPropFeePreBill.getPreBillID());

		a.setOperation("2");
		a.setLogType(PropFeeAmountLogDto.CANCEL_PREBILL);
		a.setStatementType("15");
		propFeeAccountService.handleAccount(a);

		this.editPropFeePreBill(oldPropFeePreBill);
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeePreBillByMap(
			Map<String, Object> propFeePreBill, int pageNum, int pageSize) {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		PageHelper.startPage(pageNum, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeePreBillSearchMapper.batchFindPropFeePreBillByMap",
				propFeePreBill);
		return list;
	}

}